package org.itboys.mobile.controller.web.order;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.order.CooOrder;
import org.itboys.mobile.entity.mysql.order.CooOrderProduct;
import org.itboys.mobile.service.mysql.web.order.CooOrderService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：zzq
 * 时间：2017/12/5
 * 描述：后台_订单
 */
@Controller
@RequestMapping("/coo/web/order")
public class CooOrderController extends BaseController{
    @Autowired
    private CooOrderService orderService;
    /**
     * 订单模块主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model,
            @RequestParam(value="returnPage",required=true) Integer returnPage){
        if(returnPage==1){
            return  "/newcoo/order/charge/index";
        }
        return "error";
    }

    /**
     * 查看页面
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(
            HttpServletRequest request,
            @RequestParam(value="id",required=true) Long id,
            @RequestParam(value="returnPage",required=true) Integer returnPage,
            Model model
    ){
        CooOrder order = orderService.findOne(id,false);
        model.addAttribute("order",order);

        if(returnPage==1){
            return  "/newcoo/order/charge/input";
        }
        return"error";
    }

    /**
     * 查询
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ) {
        try {

            PageResult<CooOrder> result = orderService.selectOrder(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/coo/web/order/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }

    /**
     * 查询订单详情（f返回数据）
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/findOne")
    public void findOne(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="orderId",required=true) Long orderId,
                        @RequestParam(value="isPackageProduct",required=false) boolean isPackageProduct
    ) {
        try {

            CooOrder result = orderService.findOne(orderId,isPackageProduct);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgResultDO(result,CommonConstants.SUCCESS));
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/coo/web/order/findOne------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
    /**
     * 确认订单
     * @param request
     * @param response
     * @param orderId

     */
    @RequestMapping("/confirm")
    public void confirm(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="orderId",required=true) Long orderId,
                        @RequestParam(value="confirmMessage",required=true) String confirmMessage
    ) {
        try {
            orderService.confirmOrder(orderId,confirmMessage);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/mdl/web/order/confirm------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }
    /**
     * 分页获取订单商品
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="orderId",required=true) Long orderId
    ) {
        try {

            PageResult<CooOrderProduct> result = orderService.selectProduct(request,orderId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/mdl/web/order/product/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }

    /**
     * 订单发货
     * @param request
     * @param response
     * @param id

     */
    @RequestMapping("/send")
    public void send(HttpServletRequest request,
                            HttpServletResponse response,
                            @RequestParam(value="id",required=true) Long id
    ) {
        try {
            orderService.sendOrder(id);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/mdl/web/order/send------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, msg));
        }

    }

}
